package com.igeek.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.car.entity.User;
import com.igeek.car.mapper.UserMapper;
import com.igeek.car.service.UserService;
import com.igeek.car.vo.PageQueryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public Page<User> findPage(PageQueryVO vo) {
        // 创建分页对象
        Page<User> page = new Page<>(vo.getCurrentPage(), vo.getPageSize());

        // 构建查询条件
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

        String queryString = vo.getQueryString();
        if (queryString != null && !queryString.trim().isEmpty()) {
            wrapper.like(User::getUsername, queryString)
                    .or()
                    .like(User::getEmail, queryString)
                    .or()
                    .like(User::getPhoneNumber, queryString);
        }

        // 按用户类型查询
        String userType = vo.getUserType();
        if (userType != null && !userType.trim().isEmpty()) {
            wrapper.eq(User::getUserType, userType);
        }

        // 按状态查询
        String status = vo.getStatus();
        if (status != null && !status.trim().isEmpty()) {
            wrapper.eq(User::getStatus, status);
        }

        // 按创建时间降序排序
        wrapper.orderByDesc(User::getCreationTime);

        return this.page(page, wrapper);
    }

    @Override
    public User findByUsername(String username) {
        if (username == null || username.trim().isEmpty()) {
            return null;
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        return this.getOne(wrapper);
    }

    @Override
    @Transactional
    public boolean updateStatus(Integer userId, String status) {
        if (userId == null || status == null || status.trim().isEmpty()) {
            return false;
        }
        User user = new User();
        user.setUserId(userId);
        user.setStatus(status);
        return this.updateById(user);
    }

    @Override
    @Transactional
    public boolean save(User user) {
        // 设置创建时间
        user.setCreationTime(LocalDateTime.now());
        // 设置初始状态
        if (user.getStatus() == null || user.getStatus().trim().isEmpty()) {
            user.setStatus("待审核");
        }
        return super.save(user);
    }

    @Override
    public User findByEmail(String email) {
        if (email == null || email.trim().isEmpty()) {
            return null;
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getEmail, email);
        return this.getOne(wrapper);
    }

    @Override
    public User findByPhone(String phone) {
        if (phone == null || phone.trim().isEmpty()) {
            return null;
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhoneNumber, phone);
        return this.getOne(wrapper);
    }
}